/************************************************
* DESCRIPTION:
*   
************************************************/
#include "event.h"
#include "assert.h"
#include "modbus.h"

/************************************************
* Declaration
************************************************/

/************************************************
* Variable 
************************************************/
static EventHandler g_event_handlers[kEventIDMax];
int g_events[kEventIDMax];

/************************************************
* Function 
************************************************/

void event_set(EventID event)
{
	assert(event < array_size(g_events));
	g_events[event] = 1;
}

void event_clear(EventID event)
{
	assert(event < array_size(g_events));
	g_events[event] = 0;
}

bool event_is_set(EventID event)
{
	assert(event < array_size(g_events));
	return g_events[event] == 1 ? true : false;
}

void event_set_handler(EventID event, EventHandler event_handler)
{
	assert(event < kEventIDMax);
	g_event_handlers[event] = event_handler;
}
void event_handle(EventID event)
{
	assert(event < kEventIDMax);
	if(g_event_handlers[event]){
		g_event_handlers[event]();
	}
}

bool event_handle_all(void)
{
	bool ret = false;
    int i = 0;

	for (i = 0; i < kEventIDMax; i++){
        if(event_is_set((EventID)i)){
            event_clear((EventID)i);
            event_handle((EventID)i);
            ret = true;
        }
    }

    RS485_Service() ;
    return ret;
}

void main_run()
{
    while(1){
        //handle all
        if (event_handle_all() == false){
			//if nothing need handle, go to sleep
		    //ssz_sleep();
        }
    }
}

